package net.whg.datasource;

import net.whg.datasource.DataSourceType;
import org.springframework.beans.factory.InitializingBean;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

/*@Component
@Primary*/
@Deprecated
public class DynamicDataSourceDeprecated implements DataSource, InitializingBean {

    //当前使用的数据源标识
    public static ThreadLocal<String> name = new ThreadLocal<>();

    //@Autowired
    private DataSource dataSource1;

    //@Autowired
    private DataSource dataSource2;

    @Override
    public Connection getConnection() throws SQLException {
        //写操作走主库
        if (name.get().equalsIgnoreCase(DataSourceType.MASTER)){
            return dataSource1.getConnection();
        }else {
            //读操作走从库
            return dataSource2.getConnection();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        //默认走主库
        name.set(DataSourceType.MASTER);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}
